package com.schedule.task.util.logfree;

import java.io.*;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class FreeWriter {

    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String fileName = "../" + simpleDateFormat.format(date) + ".log";
        try {
            FreeWriter.logFree(fileName, "springboot");
            FreeWriter.logFree(fileName, "flink");
            FreeWriter.logFree(fileName, "spark");
            FreeWriter.logFree(fileName, "hbase");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void logFree(String fileName, String line) throws IOException {
        //Date date = new Date();
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //String currentTime =  simpleDateFormat.format(date);
        Date date = new Date();
        String s = "yyyy-MM-dd HH:mm:ss:SSS";
        LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        String formatTime = localDateTime.format(DateTimeFormatter.ofPattern(s));
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true), "UTF-8"));
        try {
            bufferedWriter.write(formatTime + ": " + line + "\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
        bufferedWriter.close();
    }
}
